package com.cqwshzj.util;/**
 * @Description:
 * @Auther: Huang Zhijin
 * @Version: 1.0.0
 * @Date: 2021/4/7 17:01
 */

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 *
 * 〈〉
 *
 * @author Huang Zhijin
 * @create 2021/4/7
 * @since 1.0.0
 */
public class CrontabUtil {

	public static final String CHAR_QUESTION_MARK = "?";
	public static final String CHAR_WILDCARD = "*";
	public static final String CHAR_SPACE = " ";
	public static final String CHAR_FORWARD_SLASH = "/";
	public static final String CHAR_COMMA = ",";

	public static final String[] DIGIT = {"0","1","2","3","4","5","6","7","8","9"};



	public static String getCrontabByLocalDateTime(LocalDateTime dateTime){
		if (dateTime == null) {
			return null;
		}
		StringBuilder sb = new StringBuilder();
		sb.append( dateTime.getSecond() ).append( CHAR_SPACE )
				.append( dateTime.getMinute() ).append( CHAR_SPACE )
				.append( dateTime.getHour() ).append( CHAR_SPACE )
				.append( dateTime.getDayOfMonth() ).append( CHAR_SPACE )
				.append( dateTime.getMonthValue() ).append( CHAR_SPACE )
				//不指定星期几
				.append( CHAR_QUESTION_MARK ).append( CHAR_SPACE )
				.append( dateTime.getYear() );
		return sb.toString();
	}


	public static String getCrontabByLocalDateTimeZeroSecond(LocalDateTime dateTime) {
		if (dateTime == null) {
			return null;
		}

		StringBuilder sb = new StringBuilder();
		sb.append( DIGIT[0] ).append( CHAR_SPACE )
				.append( dateTime.getMinute() ).append( CHAR_SPACE )
				.append( dateTime.getHour() ).append( CHAR_SPACE )
				.append( dateTime.getDayOfMonth() ).append( CHAR_SPACE )
				.append( dateTime.getMonthValue()  ).append( CHAR_SPACE )
				//不指定星期几
				.append( CHAR_QUESTION_MARK ).append( CHAR_SPACE )
				.append( dateTime.getYear() );

		return sb.toString();
	}

	/**
	 *
	 * @param localDate
	 * @param minInterval
	 * @param hourBegin
	 * @param hourEnd
	 * @return
	 */
	public static String getIntervalMinuteCrontabOfTargetDate(LocalDate localDate, int minInterval, int hourBegin, int hourEnd ){
		if (localDate == null || minInterval <=0
			|| hourBegin <0 || hourEnd <0 || hourBegin >=24 || hourEnd >= 24) {
			return  null;
		}
		if (hourBegin > hourEnd) {
			return null;
		}

		StringBuilder sb = new StringBuilder();
		sb.append( 0 ).append( CHAR_SPACE );
		//拼接 分钟周期数
		sb.append( 0 ).append( CHAR_FORWARD_SLASH ).append( minInterval ).append( CHAR_SPACE );

		for (int i = hourBegin; i <= hourEnd; i++) {
			if ( i > hourBegin ){
				sb.append( CHAR_COMMA );
			}
			sb.append(i);
		}
		sb.append( CHAR_COMMA );

		sb.append( localDate.getDayOfMonth() ) .append( CHAR_SPACE )
				.append( localDate.getMonthValue() ).append( CHAR_SPACE )
				//不指定周几
				.append( CHAR_QUESTION_MARK ).append( CHAR_SPACE )
				.append( localDate.getYear() );

		return  sb.toString();
	}

}
